package com.huhao.wshare.config;

import com.huhao.wshare.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component // 将这个类注册为 Spring 组件，以便被 Spring Boot 发现和管理
public class DatabaseStateInitializer implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(DatabaseStateInitializer.class);

    private final UserRepository userRepository;

    @Autowired
    public DatabaseStateInitializer(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("应用启动，开始重置所有用户的登录状态...");
        try {
            userRepository.resetAllLoginStatuses();
            logger.info("所有用户的登录状态已成功重置为 0 (未登录)。");
        } catch (Exception e) {
            logger.error("重置用户登录状态时发生错误: ", e);
        }
    }
}
